﻿using UnityEditor;
using UnityEngine.SceneManagement;
using UnityEngine;
using UnityToolbarExtender;
using UnitEditorExpand.EditScene;
using System;
using System.Diagnostics;
using UnityEditor.SceneManagement;


namespace UnitEditorExpand
{
    /// <summary>
    /// 
    /// </summary>
    /// 
    [InitializeOnLoad]
    public static class ModuleTopToolBarSceneMenu
    {
        static ModuleTopToolBarSceneMenu()
        {
            EditorApplication.playModeStateChanged += OnPlayModeChanged;
            EditorApplication.pauseStateChanged += OnPauseChanged;


            ToolbarExtender.RightToolbarGUI.Add(OnToolbarGUI);
            ToolbarExtender.RightToolbarGUI.Add(OnToolbarGUIPing);
            ToolbarExtender.RightToolbarGUI.Add(OnToolbarGUICommit);
        }

   

        static void OnPlayModeChanged(PlayModeStateChange arg)
        {
            if (arg == PlayModeStateChange.EnteredPlayMode)
            {
                EditorWindow.FocusWindowIfItsOpen<SceneView>();
            }
        }
        static void OnPauseChanged(PauseState arg)
        {
            if (arg == PauseState.Unpaused)
            {

                EditorApplication.delayCall += EditorWindow.FocusWindowIfItsOpen<SceneView>;
            }
        }



        static void OnToolbarGUI()
        {
            bool isCLick = GUILayout.Button($"{SceneManager.GetActiveScene().name}", new GUILayoutOption[] { GUILayout.Width(20), GUILayout.MaxWidth(200) });
            if (isCLick)
            {
                GUIStyle style = new GUIStyle(EditorStyles.toolbarButton)
                {
                    fixedHeight = 20f,
                    fontSize = 10,
                    alignment = TextAnchor.MiddleLeft,
                    padding = new RectOffset(10, 10, 20, 0)
                };

                EditorSceneUtil.DOMenuActive(GUILayoutUtility.GetRect(200, 50, 0, 0, style));
            }
        }

        private static void OnToolbarGUIPing()
        {
            bool isCLick = GUILayout.Button("🔨", new GUILayoutOption[] { GUILayout.Width(20), GUILayout.MaxWidth(20) });
            if (isCLick)
            {
                EditorSceneUtil.DOSceneLocate(SceneManager.GetActiveScene().path);
            }
        }

        private static void OnToolbarGUICommit()
        {
            bool isCLick = GUILayout.Button("Commit", new GUILayoutOption[] { GUILayout.Width(20), GUILayout.MaxWidth(20) });
            if (isCLick)
            {

            }
        }
    }
}
